package com.wephone.Student.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.loopj.android.http.RequestParams;
import com.wephone.Student.R;
import com.wephone.Student.bean.BaseResponse;
import com.wephone.Student.bean.data.FanliDetailReq;
import com.wephone.Student.network.HttpClient;
import com.wephone.Student.network.MyResponseHandler;
import com.wephone.Student.util.Constants;
import com.wephone.Student.util.ShareReferenceUtil;

public class FanliDetail4Activity extends BaseActivity {
	private TextView titleText;
	private EditText fanliEdit1;
	private EditText fanliEdit2;
	private int type;

	private MyResponseHandler getHandler(){
		MyResponseHandler handler = new MyResponseHandler(FanliDetail4Activity.this,BaseResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(getDataSuccess){
					ShareReferenceUtil.saveApplyIds(getApplicationContext(), -1);
					Toast.makeText(getApplicationContext(), "返利信息已提交", Toast.LENGTH_SHORT).show();
					setResult(11);
					finish();
				}
			}
		};
		return handler;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fanli_detail_4);
		
		init();
	}
	
	private void init(){
		titleText = (TextView)findViewById(R.id.title_text);
		fanliEdit1 = (EditText)findViewById(R.id.fanli_edit_1);
		fanliEdit2 = (EditText)findViewById(R.id.fanli_edit_2);
		
		type = getIntent().getIntExtra("type",-1);
		if(type==1){
			titleText.setText("银联");
			fanliEdit1.setHint("请输入储蓄卡卡号");
			fanliEdit2.setHint("请输入持卡人姓名");
		}else if(type==2){
			titleText.setText("支付宝");
			fanliEdit1.setHint("请输入支付宝账号");
			fanliEdit2.setHint("请输入再次输入支付宝账号");
			((TextView)findViewById(R.id.fanli_text_2)).setText("账号确认");
		}else if(type==3){
			titleText.setText("Paypal");
			fanliEdit1.setHint("请输入Paypal账号");
			fanliEdit2.setHint("请输入再次输入Paypal账号");
			((TextView)findViewById(R.id.fanli_text_2)).setText("账号确认");
		}
		
		findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		findViewById(R.id.btn_finish).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(type==1){
					if(TextUtils.isEmpty(fanliEdit1.getText().toString())){
						Toast.makeText(getApplicationContext(), "储蓄卡卡号必须填写", Toast.LENGTH_SHORT).show();
					}else if(TextUtils.isEmpty(fanliEdit2.getText().toString())){
						Toast.makeText(getApplicationContext(), "持卡人姓名必须填写", Toast.LENGTH_SHORT).show();
					}else{
						RequestParams params = new RequestParams();
						params.put("rUid", BaseActivity.getMyInfo(getApplicationContext()).getuId());
						FanliDetailReq detailReq = new FanliDetailReq();
						detailReq.setType("现金返利");
						detailReq.setChannel("银行卡付款");
						detailReq.setUnionpay_acc(fanliEdit1.getText().toString());
						detailReq.setUnionpay_name(fanliEdit2.getText().toString());
						Gson gson = new Gson();
						params.put("rDetail", gson.toJson(detailReq));
						
						HttpClient.getInstance().post(Constants.API_ADD_REBATE_INFO_URL, params, getHandler());
					}
				}else if(type==2){
					if(TextUtils.isEmpty(fanliEdit1.getText().toString())){
						Toast.makeText(getApplicationContext(), "支付宝账号必须填写", Toast.LENGTH_SHORT).show();
					}else if(TextUtils.isEmpty(fanliEdit2.getText().toString())){
						Toast.makeText(getApplicationContext(), "再次输入支付宝账号必须填写", Toast.LENGTH_SHORT).show();
					}else if(!fanliEdit1.getText().toString().equals(fanliEdit2.getText().toString())){
						Toast.makeText(getApplicationContext(), "两次输入的信息必须相同", Toast.LENGTH_SHORT).show();
					}else{
						RequestParams params = new RequestParams();
						params.put("rUid", BaseActivity.getMyInfo(getApplicationContext()).getuId());
						FanliDetailReq detailReq = new FanliDetailReq();
						detailReq.setType("现金返利");
						detailReq.setChannel("支付宝付款");
						detailReq.setAlipay_acc(fanliEdit1.getText().toString());
						Gson gson = new Gson();
						params.put("rDetail", gson.toJson(detailReq));
						
						HttpClient.getInstance().post(Constants.API_ADD_REBATE_INFO_URL, params, getHandler());
					}
				}else if(type==3){
					if(TextUtils.isEmpty(fanliEdit1.getText().toString())){
						Toast.makeText(getApplicationContext(), "Paypal账号必须填写", Toast.LENGTH_SHORT).show();
					}else if(TextUtils.isEmpty(fanliEdit2.getText().toString())){
						Toast.makeText(getApplicationContext(), "再次输入Paypal账号必须填写", Toast.LENGTH_SHORT).show();
					}else if(!fanliEdit1.getText().toString().equals(fanliEdit2.getText().toString())){
						Toast.makeText(getApplicationContext(), "两次输入的信息必须相同", Toast.LENGTH_SHORT).show();
					}else{
						RequestParams params = new RequestParams();
						params.put("rUid", BaseActivity.getMyInfo(getApplicationContext()).getuId());
						FanliDetailReq detailReq = new FanliDetailReq();
						detailReq.setType("现金返利");
						detailReq.setChannel("Paypal付款");
						detailReq.setPaypal_acc(fanliEdit1.getText().toString());
						Gson gson = new Gson();
						params.put("rDetail", gson.toJson(detailReq));
						
						HttpClient.getInstance().post(Constants.API_ADD_REBATE_INFO_URL, params, getHandler());
					}
				}
			}
		});
	}
	

}
